<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class PvsysController extends AdminbaseController {
    public function index() {
        session_start();
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by id';
        
        $postData=array();
        $where='';
        $postData['p']=$p;
        $Model = M();
        
        if (isset($_GET['gid'])){
            $_SESSION['gameid']=$_GET['gid'];
        }
        $gameInfo=$Model->query("SELECT id,name FROM `z_game`   ");
        if (isset($_SESSION['gameid'])){
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where id=".$_SESSION['gameid']." limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }else{
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }
         
        $gameId=$queryTable[0]['id'];
        $gameName=$queryTable[0]['name'];
        
        $result = $Model->query("SHOW TABLES LIKE '".$tableName."_team' ");
        if (empty($result)){
            $tableName='y1'; //表不存在
            $where=" where id='0' ";
        }
         
        $query=$Model->query("SELECT count(id) as count FROM `".$tableName."_pvuv` ".$where."  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $uInfo=$Model->query("SELECT * FROM `".$tableName."_pvuv` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        $theData=array();
        foreach ($uInfo as $v){
            $theData[]=$v;
        }
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("thedata", $theData);
        $this->assign("gameName", $gameName);
        $this->assign("gameId", $gameId);
        $this->assign("gameInfo", $gameInfo);
        

        $this->display();
    }
    
    public function add() {
        
        
        $this->display();
    }
    
   
    
    public function delete() {
         
    }
  
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        $thedata=$Model->query("SELECT * FROM `z_game` where id='".$id."' limit 0,1  ");
        $this->assign("p", $p);
        $this->assign("thedata", $thedata[0]);
         
        $this->display();
    }
     
    public function add_post() {
        if (isset($_POST['name']) && !empty($_POST['name'])){
            $Model = M("game", 'z_');
            $data=array();
            $data['name'] = addslashes(trim($_POST['name']));
            $data['starttime'] = addslashes(trim($_POST['starttime']));
            $data['endtime'] = addslashes(trim($_POST['endtime']));
            $data['topnum'] = (int)trim($_POST['topnum']);
            $data['sharetitle'] = addslashes(trim($_POST['sharetitle']));
            $data['sharedesc'] = addslashes(trim($_POST['sharedesc']));
            $data['shareimg'] = $_POST['smeta']['thumb'];
            $data['shuoming'] = addslashes($_POST['post']['post_content']);
            $data['createtime'] = date("Y-m-d H:i:s");
            
            if($data['topnum'] < 1){
                $data['topnum'] =1;
            }
            
            $Model->add($data);
            $info=json_encode(array('status'=>'success'));
            echo $info;
        }
    }
    
    public function editpost() {
 
        if (isset($_POST['name']) && isset($_POST['option_id'])){
            $Model = M("game", 'z_');
            $data=array();
            $data['name'] = addslashes(trim($_POST['name']));
            $data['starttime'] = addslashes(trim($_POST['starttime']));
            $data['endtime'] = addslashes(trim($_POST['endtime']));
            $data['shareimg'] = $_POST['thumb'];
            $data['shuoming'] = addslashes($_POST['post']['post_content']);
            $data['createtime'] = date("Y-m-d H:i:s");
            $id=$_POST['option_id'];
            $status=$Model->where('id='.$id)->save($data);
 
            //$sql=$Model->getLastSql();
          
            
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
